package com.imooc.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.imooc.api.service.BaseService;
import com.imooc.enums.Sex;
import com.imooc.pojo.AppUser;
import com.imooc.pojo.Fans;
import com.imooc.pojo.vo.RegionRatioVO;
import com.imooc.user.mapper.FansMapper;
import com.imooc.user.service.MyFansService;
import com.imooc.user.service.UserService;
import com.imooc.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.java2d.pipe.RegionClipSpanIterator;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyFansServiceImpl extends BaseService implements MyFansService {

    @Autowired
    private FansMapper fansMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private Sid sid;
    @Override
    public boolean isMeFollowThisWriter(String fanId, String writerId) {
        Fans fans = new Fans();
        fans.setFanId(fanId);
        fans.setWriterId(writerId);
        int count = fansMapper.selectCount(fans);
        return count > 0 ? true : false;
    }

    @Override
    public void follow(String writerId, String fanId) {
        //1.查询粉丝的信息
        AppUser user = userService.getUser(fanId);
        //2.设置粉丝表的信息，保存到数据库中
        Fans fans = new Fans();
        String fanPkId = sid.nextShort();
        fans.setId(fanPkId);
        fans.setWriterId(writerId);
        fans.setFanId(fanId);
        fans.setFace(user.getFace());
        fans.setFanNickname(user.getNickname());
        fans.setProvince(user.getProvince());
        fans.setSex(user.getSex());
        fansMapper.insert(fans);
        //3.作家粉丝+1，用户的关注人数+1
        redis.increment(REDIS_WRITER_FANS_COUNTS + ":" + writerId, 1);
        redis.increment(REDIS_MY_FOLLOW_COUNTS + ":" + fanId, 1);

    }
    @Override
    public void unfollow(String writerId, String fanId) {
        //1.删除粉丝表中关联关系
        Fans fans = new Fans();
        fans.setFanId(fanId);
        fans.setWriterId(writerId);
        fansMapper.delete(fans);
        //2.redis中的数据-1
        redis.decrement(REDIS_WRITER_FANS_COUNTS + ":" + writerId, 1);
        redis.decrement(REDIS_MY_FOLLOW_COUNTS + ":" + fanId, 1);
    }

    @Override
    public PagedGridResult queryAll(String writerId,Integer page,Integer pageSize) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);
        PageHelper.startPage(page, pageSize);
        List<Fans> fansList = fansMapper.select(fans);
        PagedGridResult pagedGridResult = pagedResultSettings(fansList, page);
        return pagedGridResult;

    }

    @Override
    public Integer queryCounts(String writerId, Sex sex) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);
        fans.setSex(sex.type);
        Integer count = fansMapper.selectCount(fans);
        return count;
    }
    @Override
    public List<RegionRatioVO> queryRatioByRegion(String writerId) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);
        List<RegionRatioVO> list = new ArrayList<>();
        for (String province : REGIONS) {
            RegionRatioVO regionRatioVO = new RegionRatioVO();
            fans.setProvince(province);
            Integer value = fansMapper.selectCount(fans);
            regionRatioVO.setValue(value);
            regionRatioVO.setName(province);
            list.add(regionRatioVO);
        }
        return list;
    }

}
